#define NUM 10000
int* largestValues(struct TreeNode* root, int* returnSize){
    int *ret = (int*)malloc(sizeof(int) * NUM);
    *returnSize = 0;
    if (root == NULL) return NULL;
    struct TreeNode *queue[NUM] = {NULL};
    int front = 0;
    int rear = 0;
    queue[rear++] = root;
    int len = (rear - front + NUM) % NUM;

    while (len > 0) {
        int max = INT_MIN;
        for (int i = 0; i < len; i++) {
            struct TreeNode *tmp = queue[front++];
            max = max > tmp->val ? max : tmp->val;
            if (tmp->left != NULL) {
                queue[rear++] = tmp->left;
            }
            if (tmp->right != NULL) {
                queue[rear++] = tmp->right;
            }
        }
        ret[(*returnSize)++] = max;
        len = (rear - front + NUM) % NUM;
    }
    return ret;
}
